/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package server;

import DB.Baza;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.Korisnik;
import model.Paket;
import model.Radnik;

/**
 *
 * @author Dejan
 */
public class Server  {

    private ServerSocket servSock;
    private Socket s;
    private ArrayList<KlijentHook> kh;
    private ArrayList<Korisnik> korisnici;
    private Baza b;
    private int count =0;
    private ObjectOutputStream out;
   private int sifra;
    private int id;

    Server() {
        try {
            //this.sf =sf;
            sifra=1;
            Korisnik k = new Korisnik("mika", "mika", "mikic", "mikac");
            korisnici = new ArrayList<Korisnik>();
            korisnici.add(k);
            b = new Baza();
            servSock = new ServerSocket(21243);
            kh = new ArrayList<KlijentHook>();
        } catch (IOException ex) {
            Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    void pokreni(){
        try {
            
            
            while(true){
                   if(count<5){
                    s = servSock.accept();
                     KlijentHook k = new KlijentHook(s, this, id);
                     k.start();
                     getKh().add(k);
                     count++;
                     id++;
                   }
            }
        }
        catch (IOException ex) {
            Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
        }


    }

    
  

                            


               // s.pokreni();
            
    

    public boolean proveri(String user, String pass) {
        for(Korisnik k:getKorisnici()){
           if(k.getUsername().equals(user)&k.getPass().equals(pass)){ return true;}
         }
        return false;
    }

    public void unesiRadnika(Radnik radnik) {
        b.otvoriBazu("jdbc:odbc:leys");
        b.sacuvajRadnika(radnik);
    }
    public ArrayList<Radnik> prikaziRadnike(){
        b.otvoriBazu("jdbc:odbc:leys");

       
        return  b.uzmiSveRadnike();
            }
    public String dajSifru(){
      b.otvoriBazu("jdbc:odbc:leys");

        String sifraN=b.dajNovuSifru();
        System.out.println(sifra+" "+sifraN);
        if(Integer.parseInt(sifraN)+1>this.sifra ){
            this.sifra=Integer.parseInt(sifraN);
            this.sifra++;
            return (""+Integer.parseInt(sifraN)+1);
        
        }else return (""+this.sifra);
    }

    public void posalji(Paket paket, Socket se) {
        try {
            System.out.println("saljem");
            out = new ObjectOutputStream(se.getOutputStream());
            out.writeObject(paket);
            out.flush();
        } catch (IOException ex) {
            Logger.getLogger(KlijentHook.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * @return the korisnici
     */
    public ArrayList<Korisnik> getKorisnici() {
        return korisnici;
    }

    /**
     * @param korisnici the korisnici to set
     */
    public void setKorisnici(ArrayList<Korisnik> korisnici) {
        this.korisnici = korisnici;
    }

    /**
     * @return the kh
     */
    public ArrayList<KlijentHook> getKh() {
        return kh;
    }

    /**
     * @param kh the kh to set
     */
    public void setKh(ArrayList<KlijentHook> kh) {
        this.kh = kh;
    }


}
